//
//  USAlbumDrawView.m
//  us
//
//  Created by lhx on 2025/5/20.
//

#import "USAlbumDrawView.h"
#import "USColorPickerView.h"
@interface USAlbumDrawView ()

 
 
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) UIButton *penBtn;
@property (nonatomic, strong) UIButton *cleanBtn;


@end

@implementation USAlbumDrawView


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupView];
 
    }
    return self;
}

- (NSMutableArray *)colorGroups {
    if (_colorGroups == nil) {
        _colorGroups = [NSMutableArray array];
        
        NSArray<NSString *> *vividMacaronColors = @[
            // 🌸 粉红
            @"#F8DCE5", @"#F1B0C6", @"#E985A6", @"#E15A87",
            // 🔴 红
            @"#F8DCDC", @"#F1B0B0", @"#E98585", @"#E15A5A",
            // 🟠 橙
            @"#F8E8DC", @"#F1CBB0", @"#E9AF85", @"#E1925A",
            // 🟡 黄
            @"#F8F4DC", @"#F1E6B0", @"#E9D885", @"#E1CB5A",
            // 🟢 绿
            @"#E1F8DC", @"#BBF1B0", @"#96E985", @"#70E15A",
            // 🔵 蓝
            @"#DCEAF8", @"#B0D1F1", @"#85B7E9", @"#5A9EE1",
            // 🟦 青
            @"#DCF8F4", @"#B0F1E6", @"#85E9D8", @"#5AE1CB",
            // 🟣 紫
            @"#EADCF8", @"#D1B0F1", @"#B785E9", @"#9E5AE1",
            // ⚫️ 灰黑
            @"#E5E5E5", @"#BFBFBF", @"#999999", @"#666666",@"#212121"
        ];
        
        NSArray<NSString *> *morandiColors = @[
            // 🌸 粉红
            @"#D7C0C8", @"#C3A1AC", @"#B9929F", @"#A57383",
            // 🔴 红
            @"#D7C0C0", @"#C3A1A1", @"#B99292", @"#A57373",
            // 🟠 橙
            @"#D7CAC0", @"#C3AFA1", @"#B9A292", @"#A58873",
            // 🟡 黄
            @"#D7D3C0", @"#C3BDA1", @"#B9B292", @"#A59D73",
            // 🟢 绿
            @"#C4D7C0", @"#A7C3A1", @"#98B992", @"#7BA573",
            // 🔵 蓝
            @"#C0CCD7", @"#A1B2C3", @"#92A5B9", @"#738CA5",
            // 🟦 青
            @"#C0D7D3", @"#A1C3BD", @"#92B9B2", @"#73A59D",
            // 🟣 紫
            @"#CCC0D7", @"#B2A1C3", @"#A592B9", @"#8C73A5",
            //⚫️ 灰黑
        ];
        [_colorGroups addObjectsFromArray:vividMacaronColors];
        [_colorGroups addObjectsFromArray:morandiColors];
    }
    return _colorGroups;
}

- (void)drawAppear{
    if (self.hidden) {
        self.hidden = NO;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(0);
        }];
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }completion:^(BOOL finished) {

        }];
    }else {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(self.jk_height);
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }completion:^(BOOL finished) {
            self.hidden = YES;
        }];
    }
}
#pragma mark - Setup

- (void)setupView {
    self.backgroundColor = [UIColor jk_colorWithHex:0xEFEFE8];    
    
    UIView *toolView = [[UIView alloc]init];
    toolView.backgroundColor = UIColor.mi_1_color;
    toolView.layer.cornerRadius = 17;
    toolView.layer.masksToBounds = YES;
    [self addSubview:toolView];
    [toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(52);
    }];
    
    UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cleanBtn setImage:[UIImage imageNamed:@"photo_draw_clear"] forState:0];
    [cleanBtn setImage:[UIImage imageNamed:@"photo_draw_clear_select"] forState:UIControlStateSelected];
    [toolView addSubview:cleanBtn];
    cleanBtn.tag = 1001;
    self.cleanBtn = cleanBtn;
    [cleanBtn addTarget:self action:@selector(drawTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [cleanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-12);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(24);
    }];
    
    UIButton *penBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [penBtn setImage:[UIImage imageNamed:@"photo_draw_pen"] forState:0];
    [penBtn setImage:[UIImage imageNamed:@"photo_draw_pen_select"] forState:UIControlStateSelected];
    [toolView addSubview:penBtn];
    penBtn.tag = 1002;
    [penBtn setSelected:YES];
    self.penBtn = penBtn;
    [penBtn addTarget:self action:@selector(drawTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [penBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(cleanBtn.mas_left).offset(-10);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(24);
    }];
    
    // 创建滑块
    self.slider = [[UISlider alloc] init];
    self.slider.minimumValue = 0.0;
    self.slider.maximumValue = 1.0;
    self.slider.value = 0.5;
    
    self.slider.minimumTrackTintColor = [UIColor jk_colorWithHex:0xA6D0FA];
    self.slider.maximumTrackTintColor = [UIColor jk_colorWithHex:0xE6E6DA];
    self.slider.backgroundColor = [UIColor clearColor];
    self.slider.layer.cornerRadius = 4;
    [self.slider setThumbImage:[UIImage imageNamed:@"photo_slider"] forState:UIControlStateNormal];

    [self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [toolView addSubview:self.slider];
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(penBtn.mas_left).offset(-16);
        make.centerY.mas_equalTo(0);
    
    }];
    
    
    UIView *colorBgView = [[UIView alloc]init];
    colorBgView.backgroundColor = UIColor.mi_1_color;

    [self addSubview:colorBgView];
    [colorBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
//        make.height.mas_equalTo(72 + self.superview.safeAreaInsets.bottom);
        make.height.mas_equalTo(72 + 30);
    }];
    
    UIButton *colorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [colorBtn setImage:[UIImage imageNamed:@"photo_color"] forState:0];
    [colorBtn addTarget:self action:@selector(chooseColorAction:) forControlEvents:UIControlEventTouchUpInside];
    [colorBgView addSubview:colorBtn];
    [colorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.width.height.mas_equalTo(28);
        make.centerY.mas_equalTo(-15);
    }];
 
    USColorPickerView *colorPickerView = [[USColorPickerView alloc]initWithFrame:CGRectMake(0, 0, self.jk_width, 22) andLeftLayout:0];
    @weakify(self)
    colorPickerView.colorSelectedBlock = ^(UIColor * _Nonnull color) {
        @strongify(self)

        //选颜色自动选择书写模式
        [self.cleanBtn setSelected:NO];
        [self.penBtn setSelected:YES];
        if (self.delegate && [self.delegate respondsToSelector:@selector(albumDrawViewdidSelectType:)]) {
            [self.delegate albumDrawViewdidSelectType:2];
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(albumDrawViewSelectColor:)]) {
            [self.delegate albumDrawViewSelectColor:color];
        }
    };
    [colorPickerView setColorHexList:[self.colorGroups copy]];
    [colorBgView addSubview:colorPickerView];
    [colorPickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(56);
        make.right.mas_offset(0);
        make.height.mas_equalTo(22);
        make.centerY.equalTo(colorBtn);
    }];
  
}

-(void)closeButtonTapped{
    [self drawAppear];
}


- (void)sliderValueChanged:(UISlider *)sender {
    NSLog(@" -- %f --- ",sender.value);
    if (self.delegate && [self.delegate respondsToSelector:@selector(albumDrawViewPenWidth:)]) {
        [self.delegate albumDrawViewPenWidth:sender.value];
    }
}


- (void)chooseColorAction:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(albumDrawViewdidChooseColor)]) {
        [self.delegate albumDrawViewdidChooseColor];
    }
}

- (void)drawTypeAction:(id)sender{
    UIButton *btn = (UIButton *)sender;
    if (btn.tag == 1001) {
        [self.cleanBtn setSelected:YES];
        [self.penBtn setSelected:NO];
    }else{
        [self.cleanBtn setSelected:NO];
        [self.penBtn setSelected:YES];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(albumDrawViewdidSelectType:)]) {
        [self.delegate albumDrawViewdidSelectType:btn.tag - 1000];
    }
}
 
@end
